//
//  PwdViewController.m
//  xiangmu
//
//  Created by apple on 2019/8/14.
//  Copyright © 2019 rzt. All rights reserved.
//

#import "PwdViewController.h"

@interface PwdViewController ()
@property(strong,nonatomic)NSMutableArray* dataArr;
@property(strong,nonatomic)NSMutableDictionary* dataDic;
@end

@implementation PwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"修改密码";
    self.dataArr=[[NSMutableArray alloc] init];
    [self.dataArr addObjectsFromArray:kUserDefaultObjectForKey(@"user")];
    
    self.dataDic=[[NSMutableDictionary alloc] init];
    self.dataDic=[NSMutableDictionary dictionaryWithDictionary:self.dataArr[self.number]];
    

    
}

- (IBAction)sureBtnTap:(id)sender {
    if (self.oldPwd.text.length==0) {
        [self.view makeToast:@"请输入旧密码"
                    duration:2.0
                    position:CSToastPositionCenter];
    }else if (self.newpasswordTextfield.text.length==0){
        [self.view makeToast:@"请输入新密码"
                    duration:2.0
                    position:CSToastPositionCenter];
    }else if (self.againPwd.text.length==0){
        [self.view makeToast:@"请确认新密码"
                    duration:2.0
                    position:CSToastPositionCenter];
    }else if (![self.againPwd.text isEqualToString:self.newpasswordTextfield.text]){
        [self.view makeToast:@"两次密码输入不一致~"
                    duration:2.0
                    position:CSToastPositionCenter];
    }else{
        if (![self.oldPwd.text isEqualToString:self.dataDic[@"password"]]) {
            [self.view makeToast:@"旧密码错误"
                        duration:2.0
                        position:CSToastPositionCenter];
        }else{
            [self.dataDic setObject:self.newpasswordTextfield.text forKey:@"password"];
            [self.dataArr removeObjectAtIndex:self.number];
            [self.dataArr insertObject:self.dataDic atIndex:self.number];
            kUserDefaultSetObjectForKey(self.dataArr, @"user");
            MBProgressHUD* HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            HUD.completionBlock = ^(){
                [self.view makeToast:@"修改成功~" duration:2.0 position:CSToastPositionCenter title:nil image:nil style:nil completion:^(BOOL didTap) {
                    [self.navigationController popViewControllerAnimated:YES];
                }];
            };
            [HUD hideAnimated:YES afterDelay:1.0];
        }
    }
}
@end
